package com.ll.blog.aspect;
import com.ll.blog.Exception.BlogException;
import com.ll.blog.annotation.CheckWord;
import com.ll.blog.common.WordFilter;
import com.ll.blog.contant.ExceptionEnum;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;

@Aspect()
@Component
@Order(1)
public class CheckWordAspect {
    @Resource
    WordFilter wordFilter;
    @Pointcut("@annotation(com.ll.blog.annotation.CheckWord)")
    public void pointcut(){}

    @Around("pointcut() && @annotation(obj)")
    public Object around(ProceedingJoinPoint joinPoint, CheckWord obj) throws Throwable {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String i = request.getParameter(obj.Field());
        boolean b = wordFilter.assertTrue(i);
        if (b){
            throw new BlogException(ExceptionEnum.CONTENT_VIOLATION);
        }
        return joinPoint.proceed();
    }

}
